package cc.vv.btpartyjob.jobactivities.activity

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import cc.vv.baselibrary.bean.base.BaseResponseObj
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.baselibrary.view.banner.ImgBannerView
import cc.vv.btpartyjob.jnimodule.binder.BaseBinder
import cc.vv.btpartyjob.jnimodule.enums.H5UrlEnum
import cc.vv.btpartyjob.jnimodule.fragment.JniDataListFragment
import cc.vv.btpartyjob.jnimodule.globle.IntentJniKey
import cc.vv.btpartyjob.jnimodule.moudel.bean.BannerBean
import cc.vv.btpartyjob.jnimodule.moudel.resp.BannerResponseObj
import cc.vv.btpartyjob.jnimodule.server.JniDataServer
import cc.vv.btpartyjob.jnimodule.server.JniStartActivity
import cc.vv.btpartyjob.jobactivities.R
import cc.vv.btpartyjob.jobactivities.delegate.HeadlineNewsActivityDelegate
import cc.vv.btpartyjob.jobactivities.globle.IntentJobKey
import cc.vv.mvp.activity.BaseActivityMVP
import cc.vv.mvp.binder.BaseDataBinder
import com.chad.library.adapter.base.BaseQuickAdapter

/**
 * 头条新闻
 * Created by Chen.qi on 2018/10/31.
 */
class HeadingNewsActivity : BaseActivityMVP<HeadlineNewsActivityDelegate>(), BaseQuickAdapter.OnItemClickListener {
    /**
     * banner头布局
     */
    private var headView: View? = null

    /**
     * type
     */
    private var type: String = "0"

    /**
     * banner所有数据集合
     */
    private var bannerList: ArrayList<BannerBean>? = null
    /**
     * banner图片集合
     */
    private var bannerPic: ArrayList<String>? = null
    /**
     * banner图片集合
     */
    private var bannerTitle: ArrayList<String>? = null

    private var chooseType: Int = 0

    private var fragment: JniDataListFragment? = null

    override fun initData() {
        bannerList = ArrayList()
        bannerPic = ArrayList()
        bannerTitle = ArrayList()
        JniDataServer.getBannerList("2", 5, true)
    }


    override fun initView(bundle: Bundle?) {
        super.initView(bundle)
        val title = intent.getStringExtra(IntentJobKey.INTENT_JOB_KEY_TITLE)
        chooseType = intent.getIntExtra("type", 0)
        when (title) {
            "宝塔头条" -> {
                type = "0"
            }
            "街道头条" -> {
                type = "1"
            }
            "社区头条" -> {
                type = "2"
            }
            "网格头条" -> {
                type = "3"
            }

        }
        viewDelegate?.setTopBarTitle(title)
        val fm = supportFragmentManager
        val ft = fm.beginTransaction()
        fragment = JniDataListFragment()
        headView = LayoutInflater.from(this).inflate(R.layout.layout_headview_banner, null)
        fragment?.setType(type)
        fragment?.setAdapterHeadView(headView)
        ft.add(R.id.fl_ahn_data, fragment)
        ft.commit()
    }

    override fun bindEvenListener() {
        viewDelegate?.setTitleBackBtnOnClick(object : BaseTopBarView.EventInterface {
            //返回
            override fun leftOnClick() {
                finish()
            }

            override fun leftCloseClick() {}
            override fun rightOnClick() {
                JniStartActivity.startIntentOrganActivity(this@HeadingNewsActivity, chooseType, chooseType, 100)
            }
        })


    }

    override fun getDelegateClass(): Class<HeadlineNewsActivityDelegate> {
        return HeadlineNewsActivityDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return BaseBinder()
    }


    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
//        JniStartActivity.startIntentWebActivity(this, "详情", "http://www.baidu.com")
        JniStartActivity.startIntentWebActivity(mContext, H5UrlEnum.头条详情, bannerList?.get(position)?.id)
    }


    override fun getData(obj: BaseResponseObj<*>?) {
        super.getData(obj)
        when (obj) {
            is BannerResponseObj -> {
                val data = obj.data ?: return
                if (obj.data.records != null) {
                    bannerPic!!.clear()
                    bannerList!!.clear()
                    bannerTitle!!.clear()
                    for (i in 0 until obj.data.records.size) {
                        val bannerBean = data.records[i]
                        bannerPic!!.add(bannerBean?.cover!!)
                        bannerTitle!!.add(bannerBean?.title!!)
                        bannerList!!.add(bannerBean)
                    }
                    viewDelegate?.setBannerData(headView, bannerPic!!, bannerTitle!!, ImgBannerView.ImgBannerClickInter { imgPath, position ->
                        JniStartActivity.startIntentWebActivity(mContext, H5UrlEnum.广告详情, bannerList?.get(position)?.id, "详情")
                    })
                }

            }
        }
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (null == data) {
            return
        }
        if (requestCode == 100 && resultCode == 101) {
            val communityId = data.getStringExtra(IntentJniKey.INTENT_KEY_ORGAN_ID)
            when (chooseType) {
                0 -> {
                    fragment?.upData("", "", "")
                }
                1 -> {
                    fragment?.upData(communityId, "", "")
                }
                2 -> {
                    fragment?.upData("", communityId, "")
                }
                3 -> {
                    fragment?.upData("", "", communityId)
                }
            }
        }
    }


}